package com.thd.springboottest.camunda.controller;

import com.thd.springboottest.camunda.dto.ProcessInstanceTaskEntity;
import com.thd.springboottest.camunda.service.WorkFlowService;
import org.camunda.bpm.engine.ProcessEngine;
import org.camunda.bpm.engine.runtime.ProcessInstance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.stream.Collectors;

/**
 * com.thd.springboottest.camunda.controller.CamundaController
 *
 * @author: wanglei62
 * @DATE: 2022/5/26 17:01
 **/
@Controller
@RequestMapping("/camunda/sendTask")
public class Camunda02sendTask {
    @Autowired
    private ProcessEngine processEngine;
    @Autowired
    private WorkFlowService workFlowService;
    /**
     * 部署
     */
    // url : http://127.0.0.1:8899/thd/camunda/sendTask/deploy
    @RequestMapping(value="/deploy",method = RequestMethod.GET)
    @ResponseBody
    public ResponseEntity<String> deploy(){
        String deployId = this.workFlowService.deploy("processes/02sendTask.bpmn");
        System.out.println("deployId:" + deployId);
        return ResponseEntity.ok("SUCCESS");
    }

    /**
     * 开启流程
     */
    // url : http://127.0.0.1:8899/thd/camunda/sendTask/startProcess/[businessKey]
    @RequestMapping(value="/startProcess/{businessKey}",method = RequestMethod.GET)
    @ResponseBody
    public ResponseEntity<String> startProcess(@PathVariable String businessKey){
        ProcessInstance pi = processEngine.getRuntimeService().startProcessInstanceByKey("SendTask",businessKey);
        System.out.println("流程实例ID:" + pi.getProcessInstanceId());
        return ResponseEntity.ok("SUCCESS");
    }


    @RequestMapping("/queryTaskByBusinessKey/{businessKey}")
    @ResponseBody
    // url : http://127.0.0.1:8899/thd/camunda/sendTask/queryTaskByBusinessKey/xxx
    public ResponseEntity<List<ProcessInstanceTaskEntity>> queryTaskByBusinessKey(@PathVariable String businessKey){
        List<ProcessInstanceTaskEntity> taskList = workFlowService.queryTaskByBusinessKey(businessKey);

        List<ProcessInstanceTaskEntity> r =
                taskList.stream().map( task -> {

                    ProcessInstanceTaskEntity taskEntity = new ProcessInstanceTaskEntity();

                    taskEntity.setTaskId(task.getTaskId());
                    taskEntity.setTaskDefKey(task.getTaskDefKey());
                    taskEntity.setTaskName(task.getTaskName());
                    taskEntity.setTaskAssignee(task.getTaskAssignee());
                    return taskEntity;
                }).collect(Collectors.toList());
        return ResponseEntity.ok(r);
    }


    @RequestMapping("/completeTask/{taskId}")
    @ResponseBody
    // url : http://127.0.0.1:8899/thd/camunda/sendTask/completeTask/xxx
    public ResponseEntity<String> completeTask(@PathVariable String taskId){
        ProcessInstanceTaskEntity task = workFlowService.queryTaskByTaskId(taskId);

        workFlowService.completeTask(taskId,null);
        return ResponseEntity.ok("SUCCESS");
    }



}
